package com.ccb.sc.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.order.mapper.ShoppingCartMapper;
import com.ccb.sc.order.vo.ShoppingCartItemVo;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.modules.goods.entity.Goods;
import com.ccb.sc.common.modules.goods.entity.GoodsSpecs;
import com.ccb.sc.common.modules.goods.enums.GoodsStatus;
import com.ccb.sc.common.modules.goods.mapper.GoodsMapper;
import com.ccb.sc.common.modules.goods.mapper.GoodsSpecsMapper;
import com.ccb.sc.common.modules.shopkeeper.entity.ShoppingCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author ken
 * @since 2020-09-24
 */
@Service
public class ShoppingCartService extends ServiceImpl<ShoppingCartMapper, ShoppingCart> {
    private GoodsSpecsMapper goodsSpecsMapper;
    private GoodsMapper goodsMapper;

    public List<ShoppingCartItemVo> list(Long memberId) {
        return this.findByMemberId(memberId, null);
    }

    public int count(Long memberId) {
        return (int) this.count(new QueryWrapper<ShoppingCart>().lambda().eq(ShoppingCart::getShopkeeperId, memberId));
    }

    public List<ShoppingCartItemVo> findByMemberId(Long memberId, Boolean selected) {
        return this.getBaseMapper().selectByMemberId(memberId, selected);
    }

    public boolean addToCart(Long memberId, Long goodsSpecsId, Integer quantity) {
        GoodsSpecs goodsSpecs = this.goodsSpecsMapper.selectById(goodsSpecsId);
        Assert.notNull(goodsSpecs, "未找到商品");
        Goods goods = this.goodsMapper.selectById(goodsSpecs.getGoodsId());
        if (goods == null || !GoodsStatus.ON_SALE.equals(goods.getStatus())) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("商品已下架"));
        }

        if (goodsSpecs.getStock() == null || goodsSpecs.getStock() <= 0) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL).message("商品库存不足"));
        }
        List<ShoppingCartItemVo> shoppingCarts = this.findByMemberId(memberId, null);
        // 最大购物车数量50
        if (shoppingCarts.size() >= 20) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("购物车已满"));
        }

        ShoppingCart shoppingCart = this.findByGoodsSpecId(memberId, goodsSpecsId);
        if (shoppingCart != null) {
            int oldQuantity = shoppingCart.getQuantity();
            if (oldQuantity + quantity > goodsSpecs.getStock()) {
                //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL).message("商品库存不足"));
            }
            shoppingCart.setQuantity(oldQuantity + quantity);
            shoppingCart.setSelected(true);
            return this.updateById(shoppingCart);
        }
        shoppingCart = new ShoppingCart();
        shoppingCart.setGoodsSpecsId(goodsSpecsId);
        shoppingCart.setMemberId(memberId);
        shoppingCart.setQuantity(quantity);
        shoppingCart.setSelected(true);
        return this.save(shoppingCart);
    }

    public boolean changeQuantity(Long memberId, Long goodsSpecsId, Integer quantity) {
        GoodsSpecs goodsSpecs = this.goodsSpecsMapper.selectById(goodsSpecsId);
        if (goodsSpecs == null) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("商品不存在"));
        }
        ShoppingCart shoppingCart = this.findByGoodsSpecId(memberId, goodsSpecsId);
        if (shoppingCart == null) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("商品未加购物车"));
        }
        //过滤掉减少购物车数量
        int oldQuantity = shoppingCart.getQuantity();
        if (quantity > oldQuantity && quantity > goodsSpecs.getStock()) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL).message(goodsSpecs.getGoodsSpec() + "库存不足"));
        }
        shoppingCart.setQuantity(quantity);
        return this.updateById(shoppingCart);
    }

    public boolean emptyCart(Long memberId) {
        return this.remove(new QueryWrapper<ShoppingCart>().lambda().eq(ShoppingCart::getMemberId, memberId));
    }

    public void changeSelect(Long memberId, Long[] goodsSpecsIds, Boolean selected) {
        this.getBaseMapper().changeSelected(memberId, goodsSpecsIds, selected);
    }

    public void changeAllSelect(Long memberId, Boolean selected) {
        this.getBaseMapper().changeAllSelected(memberId, selected);
    }

    /**
     * 根据用户id和商品规格id查找购物车
     * @param memberId 用户id
     * @param goodsSpecsId 商品规格id
     * @return ShoppingCart
     */
    private ShoppingCart findByGoodsSpecId(Long memberId, Long goodsSpecsId) {
        return this.getOne(new QueryWrapper<ShoppingCart>().lambda()
                .eq(ShoppingCart::getMemberId, memberId)
                .eq(ShoppingCart::getGoodsSpecsId, goodsSpecsId));
    }

    @Autowired
    public void setGoodsSpecsMapper(GoodsSpecsMapper goodsSpecsMapper) {
        this.goodsSpecsMapper = goodsSpecsMapper;
    }

    @Autowired
    public void setGoodsMapper(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

}
